package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.ActiveOrderDetailRequest;
import com.yidiandao.business.data.request.ActiveOrderRequest;
import com.yidiandao.business.data.response.ActiveOrderDetailResponse;
import com.yidiandao.business.data.response.ActiveOrderResponse;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 活动订单:
 * <p>
 * Created by SunDing(dindding@163.com) on 2017/4/21 14:05
 */
public class ActiveOrderModel implements IActiveOrderModel {
    NetBiz mNetBiz = new NetBiz();

    @Override
    public void getPageData(String tag, String type, final OnLoadListener<List<ActiveOrderResponse.DataBean.EnrollListBean>> listener) {
        ActiveOrderRequest request = new ActiveOrderRequest();
        request.setUrl(Urls.ENROLL_LIST);
        request.setTag(tag);
        request.setState(type);
        request.setPageSize(1000);
        request.setPage(1);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(request, ActiveOrderResponse.class, new NetListener<ActiveOrderResponse>() {
            @Override
            public void onSuccess(ActiveOrderResponse activeOrderResponse) {
                ActiveOrderResponse.DataBean bean = activeOrderResponse.getData();
                List<ActiveOrderResponse.DataBean.EnrollListBean> enrollList = bean.getEnrollList();
                if(null!=listener){
                    listener.onSuccess(enrollList);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (null != listener) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (null != listener) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void getDetailData(String tag, String recId, final OnLoadListener<ActiveOrderDetailResponse.DataBean.EnrollDetailBean> listener) {
        ActiveOrderDetailRequest request = new ActiveOrderDetailRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setRecId(recId);
        request.setTag(tag);
        request.setUrl(Urls.ENROLL_DETAIL);
        mNetBiz.sendGetRequest(request, ActiveOrderDetailResponse.class, new NetListener<ActiveOrderDetailResponse>() {
            @Override
            public void onSuccess(ActiveOrderDetailResponse activeOrderDetailResponse) {
                if (null != listener) {
                    listener.onSuccess(activeOrderDetailResponse.getData().getEnrollDetail());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (null != listener) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (null != listener) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

}
